package alo.p19;

/**
 * @ClassName FindApplesCount
 * @Description 通过路径找到最多的苹果
 * @Author Fanxc
 * @DATE 2021/3/15 18:28
 * @Version 1.0
 */
public class FindApplesCount {
    int findApplesCount(int[][] A,int n,int m){
        int[][] S=new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                S[i][j]=A[i][j];
                //两次比较和赋值，会留下最大的那个
                if (j>0&&S[i][j]<S[i][j]+S[i][j-1]){
                    S[i][j]+=S[i][j-1];
                }
                if (i>0&&S[i][j]<S[i][j]+S[i-1][j]){
                    S[i][j]+=S[i-1][j];
                }
            }
        }
        return S[n-1][m-1];
    }
}
